﻿' =================================================================================
' Vault Utility Tool
' by Yoann Maingon - Prodeos - www.prodeos.com
' with the help of :
'   
'   Peter SCHROER           [Aras Corp]
'   Pascal MORENTON         [Ecole Centrale Paris]
'
'==================================================================================



Public Class Main


    Private Sub B_SearchFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_SearchFile.Click
        OpenFileDialog1.ShowDialog()
        T_UploadFile.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub B_SearchFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_SearchFolder.Click
        FolderBrowserDialog1.ShowDialog()
        T_DownFolder.Text = FolderBrowserDialog1.SelectedPath

    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    End Sub

    Private Sub B_RefreshDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim soapcallitem As SOAPExchanges = New SOAPExchanges
        soapcallitem.server = T_Server.Text
        Dim RecupinnovatorConf As MSXML2.DOMDocument = New MSXML2.DOMDocument
        RecupinnovatorConf = soapcallitem.GetArasDBnames


    End Sub

    Private Sub StartUp_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartUp.Click


        Dim Connector As ConnecteurAras = New ConnecteurAras
        Connector.pass = MD5CalcString(T_VadminPass.Text)
        Connector.InnovUser = T_VadminLogin.Text
        Connector.url = T_Server.Text
        Connector.db = T_Database.Text


        If (T_UploadFile.Text <> "") Then
            Try
                Connector.addsimplefile(T_UploadFile.Text, ComboBox1.SelectedValue)
                MsgBox(T_UploadFile.Text & "as been correctly uploaded to the " & ComboBox1.Text & " vault")
            Catch ex As Exception
                MsgBox("error while trying to upload")
            End Try
            
        End If



    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        StartDown.Enabled = False
        StartUp.Enabled = False
        RefreshFiles.Enabled = False
        MenuStrip1.Show()
        reload_combobox()


        PictureBox3.Image = VaultSoft.My.Resources.Resources.YellowLight
    End Sub

    Private Sub StartDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartDown.Click

        Dim destfolder As String = T_DownFolder.Text
        Dim fileID As String = Combo_VaultFiles.SelectedValue
        Dim filename As String = Combo_VaultFiles.Text

        Dim Connector As ConnecteurAras = New ConnecteurAras
        Connector.pass = MD5CalcString(T_VadminPass.Text)
        Connector.InnovUser = T_VadminLogin.Text
        Connector.url = T_Server.Text
        Connector.db = T_Database.Text


        If (T_UploadFile.Text <> "") Then
            Try
                Connector.Downloadsimplefile()
                MsgBox(T_UploadFile.Text & "as been correctly uploaded to the " & ComboBox1.Text & " vault")
            Catch ex As Exception
                MsgBox("error while trying to upload")
            End Try

        End If

        ' test for Check-out on download



    End Sub

    Private Sub RefreshFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshFiles.Click
        Dim Connector As SOAPExchanges = New SOAPExchanges
        Dim listfiles As Xml.XmlNodeList

        Connector.PW = MD5CalcString(T_VadminPass.Text)
        Connector.Login = T_VadminLogin.Text
        Connector.server = T_Server.Text
        Connector.database = T_Database.Text
        Dim tab(1) As String

        Try

            listfiles = Connector.GetVaultFile
        Catch ex As Exception
            MsgBox("erreur de connexion")
            GoTo Erreur
        End Try


        Dim noeud As Xml.XmlNode
        Dim dt As New DataTable
        dt.Columns.Add("filename")
        dt.Columns.Add("id")

        For Each noeud In listfiles
            dt.Rows.Add(noeud.Item("filename").InnerText, noeud.Item("id").InnerText)
        Next
        Combo_VaultFiles.DataSource = dt
        Combo_VaultFiles.ValueMember = "id"
        Combo_VaultFiles.DisplayMember = "filename"

Erreur:


    End Sub

    Private Sub T_DownFolder_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T_DownFolder.TextChanged
        If T_DownFolder.Text <> "" Then
            StartDown.Enabled = True
        Else
            StartDown.Enabled = False
        End If
    End Sub

    Private Sub T_UploadFile_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T_UploadFile.TextChanged

        If T_UploadFile.Text <> "" And T_VadminPass.Text <> "" Then
            StartUp.Enabled = True
        Else
            StartUp.Enabled = False
        End If

    End Sub

    Private Sub T_VadminPass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T_VadminPass.TextChanged



    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

        StartProcess("http://www.prodeos.com")

    End Sub

    Public Function StartProcess(ByVal cmd As String, Optional ByVal parameters As String = "") As Process
        If parameters = String.Empty Then
            Return Process.Start(cmd)
        Else
            Return Process.Start(cmd, parameters)
        End If
    End Function

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click

        StartProcess("http://www.aras.com")

    End Sub







    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Connector As SOAPExchanges = New SOAPExchanges
        Dim listfiles As Xml.XmlDocument



        Connector.PW = MD5CalcString(T_VadminPass.Text)
        Connector.Login = T_VadminLogin.Text
        Connector.server = T_Server.Text
        Connector.database = T_Database.Text
        Dim tab(1) As String

        Try
            listfiles = Connector.SendSoap("<Item type='project' action='get'/>")
            tab(1) = ""
            If listfiles.GetElementsByTagName("Result").Count > 0 Then

            Else

                PictureBox3.Image = VaultSoft.My.Resources.Resources.RedLight
                RefreshFiles.Enabled = False
                GoTo Erreur
            End If

            Load_Vaults()

        Catch ex As Exception
            MsgBox(ex.Message)
            PictureBox3.Image = VaultSoft.My.Resources.Resources.RedLight
            RefreshFiles.Enabled = False
            GoTo Erreur
        End Try

        PictureBox3.Image = VaultSoft.My.Resources.Resources.GreenLight

        RefreshFiles.Enabled = True



erreur:


    End Sub

    Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

    End Sub

    Private Sub ContactToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactToolStripMenuItem1.Click
        AboutBox1.Show()
    End Sub


    Private Sub HelpToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem1.Click

        Process.Start(System.IO.Path.Combine(Application.StartupPath, "vault_utility.chm"))
       
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'save the profile
        Dim nodestring As String
        Dim XMLenvelop As Xml.XmlDocument
        XMLenvelop = getProfilEnveloppe()

        nodestring = "<Profil name='" & Me.TextBox1.Text & "'>" & _
                      "<URL>" & Me.T_Server.Text & "</URL>" & _
                      "<DB>" & Me.T_Database.Text & "</DB>" & _
                      "<Login>" & Me.T_VadminLogin.Text & "</Login>" & _
                      "<PW>" & Me.T_VadminPass.Text & "</PW>" & _
                      "</Profil>"

        XMLenvelop = AddProfile(nodestring, XMLenvelop)
        XMLenvelop.Save("Profils.xml")
        reload_combobox()
    End Sub

    Public Function getProfilEnveloppe() As Xml.XmlDocument
        Dim XMIEnveloppe As Xml.XmlDocument = New Xml.XmlDocument

        XMIEnveloppe.Load("Profils.xml")

        Return XMIEnveloppe

    End Function


    Public Function AddProfile(ByVal NodeString As String, ByVal XMIenvelop As Xml.XmlDocument) As Xml.XmlDocument



        ' Ajout de l'élément créé à l'enveloppe
        Dim ItemTypesN As Xml.XmlDocumentFragment

        ItemTypesN = XMIenvelop.CreateDocumentFragment
        ItemTypesN.InnerXml = NodeString
        XMIenvelop.GetElementsByTagName("Profiles").Item(0).AppendChild(ItemTypesN)


        Return XMIenvelop

    End Function

    Private Sub reload_combobox()
        'remplir liste des profils
        Dim XMLprofils As Xml.XmlDocument
        XMLprofils = New Xml.XmlDocument

        ComboBox2.Items.Clear()

        XMLprofils.Load("Profils.xml")
        Dim listenoeuds As Xml.XmlNodeList
        Dim noeuds As Xml.XmlNode
        listenoeuds = XMLprofils.ChildNodes.Item(0).ChildNodes
        For Each noeuds In listenoeuds
            ComboBox2.Items.Add(noeuds.Attributes("name").Value)
        Next

        ComboBox2.Text = ComboBox2.Items(0)


    End Sub

    Private Sub Load_Vaults()
        Dim Connector As SOAPExchanges = New SOAPExchanges
        Dim listfiles As Xml.XmlNodeList

        Connector.PW = MD5CalcString(T_VadminPass.Text)
        Connector.Login = T_VadminLogin.Text
        Connector.server = T_Server.Text
        Connector.database = T_Database.Text
        Dim tab(1) As String

        Try

            listfiles = Connector.GetVaultNames
        Catch ex As Exception
            MsgBox("erreur de connexion")
            GoTo Erreur
        End Try


        Dim noeud As Xml.XmlNode
        Dim dt As New DataTable
        dt.Columns.Add("name")
        dt.Columns.Add("id")

        For Each noeud In listfiles
            dt.Rows.Add(noeud.Item("name").InnerText, noeud.Item("id").InnerText)
        Next
        ComboBox1.Enabled = True

        ComboBox1.DataSource = dt
        ComboBox1.ValueMember = "id"
        ComboBox1.DisplayMember = "name"

Erreur:



    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'supprimer l'élément selectionné
        Dim Del_Element As String
        Del_Element = ComboBox2.Text

        If Del_Element = "Blank" Then
            MsgBox("Blank Profile can't be deleted")
        Else
            'remplir liste des profils
            Dim XMLprofils As Xml.XmlDocument
            XMLprofils = New Xml.XmlDocument

            XMLprofils.Load("Profils.xml")
            Dim listenoeuds As Xml.XmlNodeList
            Dim noeuds As Xml.XmlNode
            listenoeuds = XMLprofils.ChildNodes.Item(0).ChildNodes
            For Each noeuds In listenoeuds
                If noeuds.Attributes("name").InnerText = Del_Element Then
                    XMLprofils.ChildNodes.Item(0).RemoveChild(noeuds)
                End If
            Next

            XMLprofils.Save("Profils.xml")
            reload_combobox()
        End If

    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        'recupérer les propriétés de l'élément selectionné
        Dim Del_Element As String
        Del_Element = ComboBox2.Text

        'remplir liste des profils
        Dim XMLprofils As Xml.XmlDocument
        XMLprofils = New Xml.XmlDocument

        XMLprofils.Load("Profils.xml")
        Dim listenoeuds As Xml.XmlNodeList
        Dim noeuds As Xml.XmlNode
        listenoeuds = XMLprofils.ChildNodes.Item(0).ChildNodes
        For Each noeuds In listenoeuds
            If noeuds.Attributes("name").Value = Del_Element Then
                Me.T_Server.Text = noeuds.ChildNodes.Item(0).InnerText
                Me.T_Database.Text = noeuds.ChildNodes.Item(1).InnerText
                Me.T_VadminLogin.Text = noeuds.ChildNodes.Item(2).InnerText
                Me.T_VadminPass.Text = noeuds.ChildNodes.Item(3).InnerText
            End If
        Next
    End Sub
End Class
